/**
 * @(#)PortalContextInterceptor.java 2011-10-21
 * 
 * Copyright 2000-2011 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */

package com.szf.base.web;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.ServletConfigInterceptor;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.szf.base.bean.SinlogConstants;
import com.szf.util.StringUtil;

public class ContextInterceptor extends AbstractInterceptor implements StrutsStatics {

	private static final long serialVersionUID = -2400234385441288077L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		final Object action = invocation.getAction();
		final ActionContext context = invocation.getInvocationContext();

		if (action instanceof BaseActionSupport) {
			HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
			((BaseActionSupport) action).setServletRequest(request);

			HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
			((BaseActionSupport) action).setServletResponse(response);

			Locale locale = (Locale) request.getAttribute(SinlogConstants.LOCALE_ATTRIBUTE);
			((BaseActionSupport) action).setLocale(locale);
			
			Principle principle = (Principle) request.getSession().getAttribute(SinlogConstants.PRINCIPLE_KEY);
			((BaseActionSupport) action).setPrinciple(principle);
		}

		String methodName = invocation.getProxy().getMethod();
		if (StringUtil.isEmpty(methodName)) {
			methodName = "execute";
		}

		return invocation.invoke();
	}

}
